package com.xjnt.base.support.admin.service;

import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xjnt.base.support.admin.entity.Permission;
import com.xjnt.base.support.admin.entity.Role;
import com.xjnt.frameworks.annotation.Service;
import com.xjnt.frameworks.core.BaseService;
import com.xjnt.frameworks.plugin.shiro.ShiroKit;
import com.xjnt.frameworks.plugin.sql.SqlKit;
import com.xjnt.frameworks.plugin.sql.criterion.Restrictions;
import com.xjnt.frameworks.plugin.sql.criterion.SqlCriteria;

/**
 * @author caoyong
 *
 */
@Service
public class PermissionService extends BaseService<Permission> {

	@Override
	public Permission getModel() {
		return Permission.me;
	}
	
	/**
	 * 权限变更后，重新加载Shiro数据权限缓存。
	 */
	@Override
	public void clearCache() {
		ShiroKit.loadJdbcAuthz(true);
	}
	
	public List<Permission> findByIds(Object[] ids) {
		SqlCriteria criteria = new SqlCriteria(getModel());
		criteria.add(Restrictions.in(Permission.PK_ID, ids));
		return getModel().find(criteria.toSql(), ids);
	}
	/**
	 * 通过角色查找权限
	 * @param roleId 角色标识
	 * @return List<Permission>
	 */
	public List<Permission> findByRoleId(String roleId){
		SqlCriteria criteria = new SqlCriteria(getModel(), "perm");
		criteria.addRelation("sys_role_permission", "srp");
		criteria.addRelation(Role.me, "role");
		criteria.add(Restrictions.eqProp("perm.uuid", "srp.permissionid"));
		criteria.add(Restrictions.eqProp("role.uuid", "srp.roleid"));
		criteria.add(Restrictions.eq("role.uuid", roleId));
		return getModel().find(criteria.toSql(), roleId);
	}

	/**
	 * 通过角色列表查找权限
	 * @param roleIds 角色标识列表
	 * @return List<Permission>
	 */
	public List<Permission> findByRoleIds(Object[] roleIds){
		SqlCriteria criteria = new SqlCriteria(getModel(), "perm");
		criteria.addRelation("sys_role_permission", "srp");
		criteria.addRelation(Role.me, "role");
		criteria.add(Restrictions.eqProp("perm.uuid", "srp.permissionid"));
		criteria.add(Restrictions.eqProp("role.uuid", "srp.roleid"));
		criteria.add(Restrictions.in("role.uuid", roleIds));
		return getModel().find(criteria.toSql(), roleIds);
	}
	
	public boolean allocPlatform(String permId, String platformId){
		int k = Db.update(SqlKit.findSql("Permission.relationPermissionAddPlatform"), permId, platformId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean removePlatformRelation(String permId){
		int k = Db.update(SqlKit.findSql("Permission.removePermissionAndPlatform"), permId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean allocMenu(String permId, String menuId){
		int k = Db.update(SqlKit.findSql("Permission.relationPermissionAddMenu"), permId, menuId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean removeMenuRelation(String permId){
		int k = Db.update(SqlKit.findSql("Permission.removePermissionAndMenu"), permId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean allocFunction(String permId, String functionId){
		int k = Db.update(SqlKit.findSql("Permission.relationPermissionAddFunction"), permId, functionId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean removeFunctionRelation(String permId){
		int k = Db.update(SqlKit.findSql("Permission.removePermissionAndFunction"), permId);
		if(k > 0){
			return true;
		}
		return false;
	}
	
	public boolean save(Permission model, String relationId) {
		boolean flag = super.save(model);
		int type = model.getInt(Permission.TYPE);
		String permId = model.getStr(Permission.PK_ID);
		switch(type){
			case 0 :
				flag = this.allocPlatform(permId, relationId);
				break;
			case 1 :
			case 2 :
				flag = this.allocMenu(permId, relationId);
				break;
			case 3 :
				flag = this.allocFunction(permId, relationId);
				break;
		}
		return flag;
	}
	
	public boolean update(Permission model, String relationId) {
		int type = model.getInt(Permission.TYPE);
		String permId = model.getStr(Permission.PK_ID);
		switch(type){
			case 0 :
				this.removePlatformRelation(permId);
				this.allocPlatform(permId, relationId);
				break;
			case 1 :
			case 2 :
				this.removeMenuRelation(permId);
				this.allocMenu(permId, relationId);
				break;
			case 3 :
				this.removeFunctionRelation(permId);
				this.allocFunction(permId, relationId);
				break;
		}
		return super.update(model);
	}
	
	/*
	 * 重写删除，同时删除关系
	 * (non-Javadoc)
	 * @see com.xjnt.frameworks.core.BaseService#delete(com.xjnt.frameworks.core.BaseModel)
	 */
	@Override
	public boolean delete(Permission model) {
		int type = model.getInt(Permission.TYPE);
		String permId = model.getStr(Permission.PK_ID);
		switch(type){
			case 0 :
				this.removePlatformRelation(permId);
				break;
			case 1 :
			case 2 :
				this.removeMenuRelation(permId);
				break;
			case 3 :
				this.removeFunctionRelation(permId);
				break;
		}
		return super.delete(model);
	}
	
	/*
	 * 重写删除，同时删除关系
	 * (non-Javadoc)
	 * @see com.xjnt.frameworks.core.BaseService#deleteById(java.lang.Object)
	 */
	@Override
	public boolean deleteById(Object id) {
		Permission model = findById(id);
		return delete(model);
	}
	
	/*==================================================
	 * 组装权限树相关查询方法
	 ===================================================*/
	/**
	 * 查找所有平台权限
	 * @return
	 */
	public List<Record> findAllPlatformPermission(){
		return Db.find(SqlKit.findSql("Permission.findAllPlatformPermission"));
	}
	
	/**
	 * 查找平台权限下的模块权限
	 * @param platformId
	 * @return
	 */
	public List<Record> findModulePermissionByPlatId(Object platformId){
		return Db.find(SqlKit.findSql("Permission.findModulePermissionByPlatId"), platformId);
	}
	
	/**
	 * 查找模块权限下的菜单权限
	 * @param moduleId
	 * @return
	 */
	public List<Record> findMenuPermissionByModId(Object moduleId){
		return Db.find(SqlKit.findSql("Permission.findMenuPermissionByModId"), moduleId);
	}
	
	/**
	 * 查找菜单权限下的功能权限
	 * @param menuId
	 * @return
	 */
	public List<Record> findFunctionPermissionByMenuId(Object menuId){
		return Db.find(SqlKit.findSql("Permission.findFunctionPermissionByMenuId"), menuId);
	}
}
